﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Liang.Device.Camera.Dll;
using Liang.Device.Model.Camera;

namespace Liang.Device.Camera.Action
{
    public sealed class WisionCamera : ICamera<Wision>
    {
        /// <summary>
        /// 静态只读实例，创建对象实例的唯一地方，加上只读属性可保证线程安全
        /// </summary>
        private static readonly WisionCamera _instance = new WisionCamera();

        /// <summary>
        /// 获取当前对象实例的唯一入口
        /// </summary>
        public static WisionCamera Instance
        {
            get { return _instance; }
        }

        /// <summary>
        /// 判断摄像头是否已初始化SDK
        /// </summary>
        /// 
        private static bool isSDKInited = false;

        public static bool IsSDKInited
        {
            get { return WisionCamera.isSDKInited; }
            set { WisionCamera.isSDKInited = value; }
        }

        /// <summary>
        /// 静态构造函数，在这里初始化摄像头SDK，仅调用一次
        /// </summary>
        static WisionCamera()
        {
            try
            {
                isSDKInited = (0 == WisionDll.KsCamera_ZYX_Init()) ? false : true;
            }
            catch (Exception e)
            {
                throw new CameraException("初始化中瀛鑫摄像头SDK失败", e.Message);
            }
        }

        /// <summary>
        /// 私有构造函数，阻止多次实例化
        /// 请通过Instance拿类的实例
        /// </summary>
        private WisionCamera()
        {
        }

        ~WisionCamera()
        {
            if (WisionCamera.IsSDKInited)
            {
                try
                {
                    WisionDll.KsCamera_ZYX_Cleanup();
                    WisionCamera.IsSDKInited = false;
                }
                catch (Exception e)
                {
                    throw new CameraException("无法注销中瀛鑫摄像头SDK", e.Message);
                }
            }
        }

        public bool Open(Wision cameraParam)
        {
            if (IntPtr.Zero != cameraParam.DevHandle)
            {
                return false;
            }

            try
            {
                cameraParam.DevHandle = WisionDll.KsCamera_ZYX_Login(cameraParam.PlayWindow,
                    cameraParam.DevName,
                    cameraParam.Ipaddr,
                    cameraParam.Port,
                    cameraParam.Accesscout,
                    cameraParam.Password,
                    cameraParam.TimeOut);
            }
            catch (Exception e)
            {
                throw new CameraException("无法登录摄像头", e.Message);
            }

            if (cameraParam.DevHandle == IntPtr.Zero)
            {
                throw new CameraException("登录摄像头失败");
            }

            return true;
        }

        public bool Play(Wision cameraParam)
        {
            if (IntPtr.Zero == cameraParam.DevHandle)
            {
                return false;
            }

            int nRealPlay = 0;
            try
            {
                nRealPlay = WisionDll.KsCamera_ZYX_RealPlay(cameraParam.DevHandle, 0,
                cameraParam.PlayWindow,
                cameraParam.PlayWindow,
                cameraParam.CodeType, cameraParam.ImageSize);
            }
            catch (Exception e)
            {
                throw new CameraException("无法播放中瀛鑫摄像头", e.Message);
            }

            if (0 == nRealPlay)
            {
                throw new CameraException("播放中瀛鑫摄像头失败");
            }

            return true;
        }

        public bool Stop(Wision cameraParam)
        {
            if (IntPtr.Zero != cameraParam.DevHandle)
            {
                if (0 == WisionDll.KsCamera_ZYX_StopRealPlay(cameraParam.DevHandle))
                {
                    throw new CameraException("无法停止播放");
                }
            }

            return true;
        }

        public bool Close(Wision cameraParam)
        {
            if (IntPtr.Zero != cameraParam.DevHandle)
            {
                if (0 == WisionDll.KsCamera_ZYX_StopRealPlay(cameraParam.DevHandle))
                {
                    throw new CameraException("无法停止播放");
                }
                else
                {
                    if (0 == WisionDll.KsCamera_ZYX_LogOut(cameraParam.DevHandle))
                    {
                        throw new CameraException("无法注销");
                    }
                    else
                    {
                        cameraParam.DevHandle = IntPtr.Zero;
                    }
                }
            }

            return true;
        }

        public bool Capture(Wision cameraParam, string strImageFile)
        {
            return (0 != WisionDll.KsCamera_ZYX_CaptureImage(cameraParam.DevHandle, strImageFile, 0));
        }
    }
}
